# convert fallbacks in pacenote table to pacenote_fallbacks table

import sqlite3
from loguru import logger

DB_PATH = '/mnt/c/Users/Admin/lab/ztmz/ztmz_pacenote/src/ZTMZ.PacenoteTool.Base/data.zdb'

def main():
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()

    res = c.execute('SELECT id, fallback FROM pacenote')
    pacenotes = res.fetchall()
    logger.info(f'loaded {len(pacenotes)} pacenotes')

    data_pacenote_fallbacks = []
    for id, fallback in pacenotes:
        if fallback is not None and fallback != '':
            fallbacks = fallback.split(',')
            data_pacenote_fallbacks.extend([(id, f, i) for i, f in enumerate(fallbacks)])
    
    logger.info(f'got {len(data_pacenote_fallbacks)} pacenote fallbacks')

    c.executemany('INSERT INTO pacenote_fallbacks(`id`, `fallback_id`, `order_id`) VALUES (?, ?, ?)', data_pacenote_fallbacks)
    conn.commit()
    logger.info('done.')


if __name__ == '__main__':
    main()
